home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / guide.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  7.7 KB  |  240 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import resources
  5. from database import DDBObject
  6. from template import fillStaticTemplate
  7. from httpclient import grabURL
  8. from urlparse import urlparse, urljoin
  9. from xhtmltools import urlencode
  10. from copy import copy
  11. from util import returnsUnicode, unicodify, checkU
  12. import re
  13. import app
  14. import config
  15. import indexes
  16. import menu
  17. import prefs
  18. import threading
  19. import urllib
  20. import eventloop
  21. import views
  22. import logging
  23. import httpclient
  24. from gtcache import gettext as _
  25. from HTMLParser import HTMLParser, HTMLParseError
  26. import iconcache
  27. HTMLPattern = re.compile('^.*(<head.*?>.*</body\\s*>)', re.S)
  28.  
  29. def isPartOfGuide(url, guideURL, allowedURLs = None):
  30.     '''Return if url is part of a channel guide where guideURL is the base URL
  31.     for that guide.
  32.     '''
  33.     if guideURL == '*':
  34.         return True
  35.     elif allowedURLs is None:
  36.         guideHost = urlparse(guideURL)[1]
  37.         urlHost = urlparse(url)[1]
  38.         return urlHost.endswith(guideHost)
  39.     elif isPartOfGuide(url, guideURL):
  40.         return True
  41.     
  42.     for altURL in allowedURLs:
  43.         if isPartOfGuide(url, altURL):
  44.             return True
  45.             continue
  46.     
  47.     return False
  48.  
  49.  
  50. class ChannelGuide(DDBObject):
  51.     ICON_CACHE_SIZES = []
  52.     
  53.     def __init__(self, url = None, allowedURLs = None):
  54.         checkU(url)
  55.         if allowedURLs is None:
  56.             self.allowedURLs = []
  57.         else:
  58.             self.allowedURLs = allowedURLs
  59.         if url is None and allowedURLs is None:
  60.             self.allowedURLs = config.get(prefs.CHANNEL_GUIDE_ALLOWED_URLS).split()
  61.         
  62.         self.allowedURLs.append(config.get(prefs.CHANNEL_GUIDE_FIRST_TIME_URL))
  63.         self.url = url
  64.         self.updated_url = url
  65.         self.title = None
  66.         self.lastVisitedURL = None
  67.         self.iconCache = iconcache.IconCache(self, is_vital = True)
  68.         self.favicon = None
  69.         self.firstTime = True
  70.         DDBObject.__init__(self)
  71.         self.downloadGuide()
  72.  
  73.     
  74.     def onRestore(self):
  75.         self.lastVisitedURL = None
  76.         if self.iconCache == None:
  77.             self.iconCache = iconcache.IconCache(self, is_vital = True)
  78.         else:
  79.             self.iconCache.dbItem = self
  80.             self.iconCache.requestUpdate(True)
  81.         if self.getDefault():
  82.             self.allowedURLs = config.get(prefs.CHANNEL_GUIDE_ALLOWED_URLS).split()
  83.             self.allowedURLs.append(config.get(prefs.CHANNEL_GUIDE_FIRST_TIME_URL))
  84.         else:
  85.             self.allowedURLs = []
  86.         self.downloadGuide()
  87.  
  88.     
  89.     def __str__(self):
  90.         return 'Miro Guide <%s>' % (self.url,)
  91.  
  92.     
  93.     def makeContextMenu(self, templateName, view):
  94.         menuItems = [
  95.             ((lambda : app.delegate.copyTextToClipboard(self.getURL())), _('Copy URL to clipboard'))]
  96.         if not self.getDefault():
  97.             i = ((lambda : app.controller.removeGuide(self)), _('Remove'))
  98.             menuItems.append(i)
  99.         
  100.         return menu.makeMenu(menuItems)
  101.  
  102.     
  103.     def remove(self):
  104.         if self.iconCache is not None:
  105.             self.iconCache.remove()
  106.             self.iconCache = None
  107.         
  108.         DDBObject.remove(self)
  109.  
  110.     
  111.     def isPartOfGuide(self, url):
  112.         return isPartOfGuide(url, self.getURL(), self.allowedURLs)
  113.  
  114.     
  115.     def getURL(self):
  116.         if self.url is not None:
  117.             return self.url
  118.         else:
  119.             return config.get(prefs.CHANNEL_GUIDE_URL)
  120.  
  121.     
  122.     def getFirstURL(self):
  123.         if self.url is not None:
  124.             return self.url
  125.         else:
  126.             return config.get(prefs.CHANNEL_GUIDE_FIRST_TIME_URL)
  127.  
  128.     
  129.     def getLastVisitedURL(self):
  130.         if self.lastVisitedURL is not None:
  131.             logging.info('First URL is %s' % self.lastVisitedURL)
  132.             return self.lastVisitedURL
  133.         elif self.firstTime:
  134.             self.firstTime = False
  135.             logging.info('First URL is %s' % self.getFirstURL())
  136.             return self.getFirstURL()
  137.         else:
  138.             logging.info('First URL is %s' % self.getURL())
  139.             return self.getURL()
  140.  
  141.     
  142.     def getDefault(self):
  143.         return self.url is None
  144.  
  145.     
  146.     def getTitle(self):
  147.         if self.title:
  148.             return self.title
  149.         elif self.getDefault():
  150.             return _('Miro Guide')
  151.         else:
  152.             return self.getURL()
  153.  
  154.     getTitle = returnsUnicode(getTitle)
  155.     
  156.     def guideDownloaded(self, info):
  157.         self.updated_url = unicode(info['updated-url'])
  158.         
  159.         try:
  160.             parser = GuideHTMLParser(self.updated_url)
  161.             parser.feed(info['body'])
  162.             parser.close()
  163.         except:
  164.             pass
  165.  
  166.         self.title = unicode(parser.title)
  167.         self.favicon = unicode(parser.favicon)
  168.         self.iconCache.requestUpdate()
  169.         self.signalChange()
  170.  
  171.     
  172.     def guideError(self, error):
  173.         pass
  174.  
  175.     
  176.     def downloadGuide(self):
  177.         httpclient.grabURL(self.getURL(), self.guideDownloaded, self.guideError)
  178.  
  179.     
  180.     def getIconURL(self):
  181.         if self.iconCache.isValid():
  182.             path = self.iconCache.getResizedFilename(20, 20)
  183.             return resources.absoluteUrl(path)
  184.         else:
  185.             return resources.url('images/channelguide-icon-tablist.png')
  186.  
  187.     getIconURL = returnsUnicode(getIconURL)
  188.     
  189.     def getThumbnailURL(self):
  190.         if self.favicon:
  191.             return self.favicon
  192.         elif self.updated_url:
  193.             parsed = urlparse(self.updated_url)
  194.         else:
  195.             parsed = urlparse(self.getURL())
  196.         return parsed[0] + u'://' + parsed[1] + u'/favicon.ico'
  197.  
  198.  
  199.  
  200. class GuideHTMLParser(HTMLParser):
  201.     
  202.     def __init__(self, url):
  203.         self.title = None
  204.         self.in_title = False
  205.         self.baseurl = url
  206.         self.favicon = None
  207.         HTMLParser.__init__(self)
  208.  
  209.     
  210.     def handle_starttag(self, tag, attrs):
  211.         attrdict = { }
  212.         for key, value in attrs:
  213.             attrdict[key] = value
  214.         
  215.         if tag == 'title' and self.title == None:
  216.             self.in_title = True
  217.             self.title = u''
  218.         
  219.         if tag == 'link' and attrdict.has_key('rel') and attrdict.has_key('type') and attrdict.has_key('href') and 'icon' in attrdict['rel'].split(' ') and attrdict['type'].startswith('image/'):
  220.             self.favicon = urljoin(self.baseurl, attrdict['href']).decode('ascii', 'ignore')
  221.         
  222.  
  223.     
  224.     def handle_data(self, data):
  225.         if self.in_title:
  226.             self.title += data
  227.         
  228.  
  229.     
  230.     def handle_endtag(self, tag):
  231.         if tag == 'title' and self.in_title:
  232.             self.in_title = False
  233.         
  234.  
  235.  
  236.  
  237. def getGuideByURL(url):
  238.     return views.guides.getItemWithIndex(indexes.guidesByURL, url)
  239.  
  240.